package com.youngjun.user.service.impl;

import com.youngjun.common.mybatis.page.PageParams;
import com.youngjun.common.mybatis.plugin.PageHelper;
import com.youngjun.user.mapper.OrganizationMapper;
import com.youngjun.user.pojo.domain.Organization;
import com.youngjun.user.pojo.dto.ProfileDTO;
import com.youngjun.user.pojo.dto.query.OrganizationQueryDTO;
import com.youngjun.user.pojo.vo.AddressVO;
import com.youngjun.user.pojo.vo.OrganizationVO;
import com.youngjun.user.pojo.vo.ProfileVO;
import com.youngjun.user.pojo.vo.UserVO;
import com.youngjun.user.service.AddressService;
import com.youngjun.user.service.OrganizationService;
import com.youngjun.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;

    @Autowired
    UserService userService;

    @Autowired
    AddressService addressService;

    @Override
    public OrganizationMapper getMapper() {
        return organizationMapper;
    }

    @Override
    public List<ProfileVO> queryList(OrganizationQueryDTO dto, PageParams pageParams) {
        PageHelper.startPage(pageParams);
        return getMapper().findAll(dto)
                .stream()
                .map(item->{
                    ProfileVO vo = new ProfileVO();

                    OrganizationVO organization =new OrganizationVO().mapFrom(item);
                    AddressVO addressVO = addressService.queryById(item.getAddressId());
                    organization.setAddress(addressVO);
                    vo.setOrganization(organization);

                    if(item.getAdminUserId() != null){
                        UserVO adminUser = userService.queryById(item.getAdminUserId());
                        vo.setUser(adminUser);
                    }

                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public ProfileVO create(ProfileDTO dto) {
        AddressVO address = addressService.create(dto.getOrganization().getAddress());

        Organization organization = new Organization().mapFrom(dto.getOrganization());
        organization.setAddressId(address.getId());
        insert(organization);

        dto.getAdminUser().setOrganizationId(organization.getId());
        UserVO adminUser = userService.create(dto.getAdminUser());

        organization.setAdminUserId(adminUser.getId());
        updateWithOptionalByPrimaryKey(organization);

        OrganizationVO vo = new OrganizationVO()
                .mapFrom(organization)
                .setAddress(address);
        return new ProfileVO()
                .setOrganization(vo)
                .setUser(adminUser);
    }

    @Override
    public ProfileVO modify(ProfileDTO dto) {
        AddressVO address = addressService.create(dto.getOrganization().getAddress());

        Organization organization = new Organization().mapFrom(dto);
        organization.setAddressId(address.getId());
        updateWithOptionalByPrimaryKey(organization);

        dto.getAdminUser().setOrganizationId(organization.getId());
        UserVO adminUser = userService.modify(dto.getAdminUser());

        OrganizationVO vo = new OrganizationVO()
                .mapFrom(organization)
                .setAddress(address);

        return new ProfileVO()
                .setOrganization(vo)
                .setUser(adminUser);
    }

}